/**
 * 城市自动定位功能测试
 * 用于验证地理位置服务的基本功能
 */

// 模拟微信小程序环境
const mockWx = {
  getLocation: (options) => {
    console.log('模拟获取位置:', options);
    // 模拟成功回调
    setTimeout(() => {
      options.success({
        latitude: 39.9042,  // 北京坐标
        longitude: 116.4074,
        accuracy: 65,
        altitude: 0,
        speed: 0
      });
    }, 1000);
  },
  showLoading: (options) => {
    console.log('显示加载:', options.title);
  },
  hideLoading: () => {
    console.log('隐藏加载');
  },
  showToast: (options) => {
    console.log('显示提示:', options.title);
  },
  showModal: (options) => {
    console.log('显示弹窗:', options.title, options.content);
    // 模拟用户确认
    setTimeout(() => {
      options.success({ confirm: true });
    }, 500);
  },
  getSetting: (options) => {
    console.log('获取设置');
    options.success({
      authSetting: {
        'scope.userLocation': true
      }
    });
  },
  openSetting: (options) => {
    console.log('打开设置');
    options.success({
      authSetting: {
        'scope.userLocation': true
      }
    });
  },
  request: (options) => {
    console.log('网络请求:', options.url);
    // 模拟逆地理编码响应
    setTimeout(() => {
      options.success({
        data: {
          status: 0,
          result: {
            address_component: {
              city: '北京市',
              district: '朝阳区',
              province: '北京市'
            }
          }
        }
      });
    }, 800);
  }
};

// 设置全局wx对象
global.wx = mockWx;

// 测试城市列表
const testCityList = [
  { id: 1, name: '北京', code: 'BJ', image_url: '/images/beijing.jpg' },
  { id: 2, name: '上海', code: 'SH', image_url: '/images/shanghai.jpg' },
  { id: 3, name: '广州', code: 'GZ', image_url: '/images/guangzhou.jpg' },
  { id: 4, name: '深圳', code: 'SZ', image_url: '/images/shenzhen.jpg' }
];

// 测试LocationService
async function testLocationService() {
  console.log('=== 开始测试地理位置服务 ===\n');
  
  try {
    // 引入LocationService（需要适配Node.js环境）
    const LocationService = require('./mini/services/location.js');
    
    // 测试1: 获取当前位置
    console.log('测试1: 获取当前位置');
    const locationRes = await LocationService.getCurrentLocation({ showLoading: false });
    console.log('位置获取结果:', locationRes);
    console.log('');
    
    // 测试2: 城市名称匹配
    console.log('测试2: 城市名称匹配');
    const matchedCity = LocationService.matchCityByName('北京', testCityList);
    console.log('匹配结果:', matchedCity);
    console.log('');
    
    // 测试3: 坐标匹配
    console.log('测试3: 坐标匹配');
    const coordMatchedCity = LocationService.matchCityByCoordinates(39.9042, 116.4074, testCityList);
    console.log('坐标匹配结果:', coordMatchedCity);
    console.log('');
    
    // 测试4: 逆地理编码
    console.log('测试4: 逆地理编码');
    const geocodeRes = await LocationService.reverseGeocode(39.9042, 116.4074);
    console.log('逆地理编码结果:', geocodeRes);
    console.log('');
    
    console.log('=== 所有测试完成 ===');
    
  } catch (error) {
    console.error('测试过程中出现错误:', error);
  }
}

// 运行测试
if (require.main === module) {
  testLocationService();
}

module.exports = {
  testLocationService,
  testCityList,
  mockWx
};
